package com.csx.governance.traffic.adm.controller;

import com.csx.governance.traffic.adm.model.bo.GrayscalePublishmentBO;
import com.csx.governance.traffic.adm.model.common.Result;
import com.csx.governance.traffic.adm.model.dto.GrayscalePublishmentDTO;
import com.csx.governance.traffic.adm.model.dto.StageTypeDTO;
import com.csx.governance.traffic.adm.model.dto.StrategyTypeDTO;
import com.csx.governance.traffic.adm.model.vo.GrayscalePublishmentVO;
import com.csx.governance.traffic.adm.model.vo.StageTypeVO;
import com.csx.governance.traffic.adm.model.vo.StrategyTypeVO;
import com.csx.governance.traffic.adm.service.GrayscalePublishService;
import com.csx.governance.traffic.adm.util.ModelConvertUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cuisongxu
 * @date 2023/5/4 15:27
 */
@Tag(name = "灰度发布控制器")
@RequestMapping("/grayscale-publish")
@RestController
@AllArgsConstructor
public class GrayscalePublishController {

    @Autowired
    private GrayscalePublishService grayscalePublishService;

    /**
     * 获取灰度发布列表
     * @return 灰度发布列表
     */
    @Operation(summary = "获取灰度发布列表")
    @GetMapping("/publishments")
    public Result<List<GrayscalePublishmentVO>> getGrayscalePublishments() {

        List<GrayscalePublishmentDTO> grayscalePublishments = grayscalePublishService.getGrayscalePublishments();
        List<GrayscalePublishmentVO> res = ModelConvertUtil.convert(grayscalePublishments, GrayscalePublishmentVO.class);
        return Result.success(res);
    }

    /**
     * 获取灰度发布
     * @return 灰度发布实体
     */
    @Operation(summary = "获取灰度发布列表")
    @GetMapping("/publishment")
    public Result<GrayscalePublishmentVO> getGrayscalePublishment(@RequestParam("publishmentName") @Parameter(description = "要获取的灰度发布的名称") String publishmentName) {

        GrayscalePublishmentDTO grayscalePublishment = grayscalePublishService.getGrayscalePublishment(publishmentName);
        GrayscalePublishmentVO res = ModelConvertUtil.convert(grayscalePublishment, GrayscalePublishmentVO.class);
        return Result.success(res);
    }

    /**
     * 新增灰度发布
     * @return 灰度发布实体
     */
    @Operation(summary = "新增灰度发布")
    @PostMapping("/add-publishment")
    public Result<Void> addGrayscalePublishment(@RequestBody @Parameter(description = "灰度发布传输实体") GrayscalePublishmentDTO grayscalePublishmentDTO) {
        GrayscalePublishmentBO grayscalePublishmentBO = ModelConvertUtil.convert(grayscalePublishmentDTO, GrayscalePublishmentBO.class);
        grayscalePublishService.addGrayscalePublishment(grayscalePublishmentBO);
        return Result.success();
    }
    /**
     * 修改灰度发布
     * @return 灰度发布实体
     */
    @Operation(summary = "修改灰度发布")
    @PostMapping("/update-publishment")
    public Result<Void> updateGrayscalePublishment(@RequestBody @Parameter(description = "灰度发布传输实体") GrayscalePublishmentDTO grayscalePublishmentDTO) {
        GrayscalePublishmentBO grayscalePublishmentBO = ModelConvertUtil.convert(grayscalePublishmentDTO, GrayscalePublishmentBO.class);
        grayscalePublishService.updateGrayscalePublishment(grayscalePublishmentBO);
        return Result.success();
    }

    /**
     * 删除灰度发布
     * @return 灰度发布实体
     */
    @Operation(summary = "删除灰度发布")
    @PostMapping("/delete-publishment")
    public Result<Void> deleteGrayscalePublishment(@RequestBody @Parameter(description = "要删除的灰度发布的名称列表") List<String> publishmentNames) {
        grayscalePublishService.deleteGrayscalePublishments(publishmentNames);
        return Result.success();
    }

    @Operation(summary = "获取发布阶段类型")
    @GetMapping("/stageTypes")
    public Result<List<StageTypeVO>> getStageTypes() {
        List<StageTypeDTO> stageTypes = grayscalePublishService.getStageTypes();
        List<StageTypeVO> res = ModelConvertUtil.convert(stageTypes, StageTypeVO.class);
        return Result.success(res);
    }

    @Operation(summary = "获取升级/回滚策略类型")
    @GetMapping("/strategyTypes")
    public Result<List<StrategyTypeVO>> getStrategyTypes() {
        List<StrategyTypeDTO> strategyTypes = grayscalePublishService.getStrategyTypes();
        List<StrategyTypeVO> res = ModelConvertUtil.convert(strategyTypes, StrategyTypeVO.class);
        return Result.success(res);
    }

}
